/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of GW-Android.

GW-Android is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

GW-Android is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GW-Android. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.android;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;
import android.widget.Toast;

public class GroupwareClient extends Activity {
	
	private ServersDbAdapter mDbAdapter;
	
	private Spinner spinner;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.server_selection);
        mDbAdapter = new ServersDbAdapter(this);
        mDbAdapter.open();
        this.fillData();
    }

    private void fillData() {
    	Cursor c = mDbAdapter.fetchAllServers();
    	startManagingCursor(c);
    	if (c.getCount() == 0) {  // TODO Preenche o servidor com dados de teste.
    		mDbAdapter.createServer("Arquigrafia", "http://www.arquigrafia.usp.br");
    		mDbAdapter.createServer("Outro servidor", "http://www.inf.ufes.br");
    		c.requery();
    	}
    	
    	// Create an array to specify the fields we want to display...
    	String[] from = new String[] { ServersDbAdapter.KEY_NAME };
    	// ...and an array of the fields we want to bind those fields to
    	int[] to = new int[]{ R.id.server_name };

    	SimpleCursorAdapter serversAdapter = new SimpleCursorAdapter(this, R.layout.server_row, c, from, to);
    	spinner = (Spinner) findViewById(R.id.change_server);
    	spinner.setAdapter(serversAdapter);
    }
    
    public static final String EXTRA_SERVER_NAME = "serverName";
    public static final String EXTRA_SERVER_URL = "serverUrl";
    public static final String EXTRA_SERVER_LOGIN = "serverLogin";
    public static final String EXTRA_SERVER_PASSWORD = "serverPassword";
    
    public void connectOnClick(View view) {
    	SQLiteCursor cursor = (SQLiteCursor) spinner.getSelectedItem();
    	if (cursor == null) {
    		Toast.makeText(this, "Selecione o servidor ao qual conectar.", Toast.LENGTH_LONG).show();
    		return;
    	}
    	
    	String serverName = cursor.getString(cursor.getColumnIndex(ServersDbAdapter.KEY_NAME));
    	String serverUrl = cursor.getString(cursor.getColumnIndex(ServersDbAdapter.KEY_URL));
    	String login = ((EditText) this.findViewById(R.id.login)).getText().toString();
    	String password = ((EditText) this.findViewById(R.id.password)).getText().toString();
    	
    	Intent intent = new Intent();
    	intent.setClass(this, MainActivity.class);
    	intent.putExtra(EXTRA_SERVER_NAME, serverName);
    	intent.putExtra(EXTRA_SERVER_URL, serverUrl);
    	intent.putExtra(EXTRA_SERVER_LOGIN, login);
    	intent.putExtra(EXTRA_SERVER_PASSWORD, password);
    	this.startActivity(intent);
    }
    
//    private static final int MENU_ADD_SERVER = 1;
//    private static final int MENU_REMOVE_SERVER = 2;
//    
//    @Override
//    public boolean onCreateOptionsMenu(Menu menu) {
//    	menu.add(0, MENU_ADD_SERVER, 0, "Adicionar servidor");
//    	menu.add(0, MENU_REMOVE_SERVER, 0, "Remover servidor");
//    	return super.onCreateOptionsMenu(menu);
//    }
//    
//    @Override
//    public boolean onOptionsItemSelected(MenuItem item) {
//    	switch (item.getItemId()) {
//    	case MENU_ADD_SERVER:
//    		// TODO
//    		return true;
//    	case MENU_REMOVE_SERVER:
//    		// TODO
//    		return true;
//    	}
//    	return super.onOptionsItemSelected(item);
//    }
}